package game;

import java.awt.*;
import java.util.*;

public abstract class Game {
	private static Map<String, Game> games = new HashMap<String, Game>();
	static {
		RegistGame("中國象棋", new ChineseChessGame());
		RegistGame("台灣暗棋", new TaiwanChessGame());
		RegistGame("五子棋", new FiveChess());
	}

	static Set<String> GetGameList() {
		return games.keySet();
	}

	static Game GetGame(String name) {
		assert games.keySet().contains(name);
		return games.get(name);
	}
	
	static Game GetGame(String name,IGameEventListener listener) {
		assert games.keySet().contains(name);
		Game game = games.get(name);
		game.setListener(listener);
		return game;
	}

	static void RegistGame(String name, Game game) {
		games.put(name, game);
	}

	protected IGameEventListener listener = new DefaultListener();
	private int currentPlayer = 0;

	protected Game() {
		super();
	}

	public abstract void Init();

	public abstract Point GetBoardSize();

	public abstract boolean IsActionableWithoutTarget(Point source);

	public abstract void Action(Point source);

	public abstract void Action(Point source, Point target);

	public abstract Set<Point> GetValidTargets(Point source);

	public abstract void PaintLocation(Graphics g, Point size, Point location);

	public boolean InRange(Point location) {
		Point size = GetBoardSize();
		return location.x >= 0 && location.x < size.x && location.y >= 0
				&& location.y < size.y;
	}
	protected int GetCurrentPlayer() {
		return currentPlayer;
	}

	protected void NextPlayer() {
		if (currentPlayer == 0)
			currentPlayer = 1;
		else
			currentPlayer = 0;
		listener.OnPlayerChange();
	}
	private void setListener(IGameEventListener listener)
	{
		this.listener = listener;
	}
	private class DefaultListener implements IGameEventListener
	{

		@Override
		public void OnGameOver() {
			System.out.println("listener.OnGameOver() called.");
		}

		@Override
		public void OnPlayerChange() {
			System.out.println("listener.OnPlayerChange() called.");
		}

		@Override
		public void OnPlayerColorChange(int playerID, Color color) {
			System.out.println("listener.OnPlayerColorChange(" + playerID + color + ") called.");
		}

	}
}
